<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class StoreBargainUser extends Model
{
    use HasFactory;
    protected $guarded = [];
    protected $table = 'store_bargain_user';

    /**
     * 砍价商品表
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function storeBargain(){
        return $this->hasOne(StoreBargain::class,'id','bargain_id');
    }

    /**
     * 用户表
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function user(){
        return $this->hasOne(User::class,'uid','uid');
    }

    /**
     * 查询用户砍价商品
     * @param $uid
     * @return \Illuminate\Database\Eloquent\Builder|Model|object|null
     */
    public static  function storeBargainUserWithStoreBargainWithUser($uid){
        return self::with(['storeBargain','user'])->where('uid',$uid)->first();
    }


    /**
     * 查询 根据用户
     * @param $uid
     * @return mixed
     */
    public static function storeBargainUserFind($uid){
        return self::where('uid',$uid)->first();
    }

    /**
     * 查询 根据ID
     * @param $id
     * @return mixed
     */
    public static function storeBargainUserFinds($id){
        return self::where('uid',$id)->first();
    }


    /**
     * 添加
     * @param $data
     * @return mixed
     */
    public static function storeBargainUserSave($data){
        return self::insert($data);
    }

    /**
     * 修改
     * @param $id
     * @param $storeBargainUser
     * @param $data
     * @return mixed
     */
    public static function storeBargainUserUpdate($id,$storeBargainUser,$data){
        return self::where('id',$id)->update([
            'price'=>$storeBargainUser+$data
        ]);
    }

    /**
     * 修改
     * @param $id
     * @param $storeBargainUser
     * @param $data
     * @return mixed
     */
    public static function storeBargainUserUpdates($id,$storeBargainUser,$data){
        return self::where('id',$id)->update([
            'status' => 3,
            'price'=>$storeBargainUser+($data-$storeBargainUser)
        ]);
    }
}
